# redis_client.py
import redis

from settings import REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD


class RedisManager:
    _instance = None
    _pool = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._init_pool()
        return cls._instance

    @classmethod
    def _init_pool(cls):
        cls._pool = redis.ConnectionPool(
            host=REDIS_HOST,
            port=REDIS_PORT,
            db=REDIS_DB,
            password=REDIS_PASSWORD,
            max_connections=20,
            decode_responses=True,
            socket_timeout=5,
            retry_on_timeout=True
        )

    @classmethod
    def get_client(cls):
        """获取Redis客户端"""
        return redis.Redis(connection_pool=cls._pool)

    @classmethod
    def close_pool(cls):
        """关闭连接池"""
        if cls._pool:
            cls._pool.disconnect()


# 使用示例
# from redis_client import RedisManager

def process_data():
    redis_client = RedisManager().get_client()
    redis_client.set('processed', 'true')
    return redis_client.get('processed')